<?php namespace app\service\controller;

use think\Controller;
use think\facade\Config;


/**
 * 微信小程序服务层
 */
class WechatMinApi extends Controller{
	
	private $appId = '';		//APPID
	private $secret = '';		//secret
	private $accessToken = '';	//accessToken
	private $url = [
		'getOpenId' 		=> 'https://api.weixin.qq.com/sns/jscode2session?appid={APPID}&secret={SECRET}&js_code={JSCODE}&grant_type=authorization_code',	//code换取openid
		'getUserPhone'		=>	'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token={ACCESS_TOKEN}',									//code换取用户手机号
		'getAccessToken'	=>	'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={APPID}&secret={APPSECRET}',					//获取accessToken
//		'getPageQrcode' 	=>	'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={ACCESS_TOKEN}',												//通过该接口生成的小程序码，永久有效，数量暂无限制
	];
	
	public function __construct(){
		parent::__construct();
		if( empty($this -> appId) ){
			$wechatCof = config('WECHAT_MIN');
			$this -> appId = $wechatCof['APPID'];
			$this -> secret = $wechatCof['SECRET'];
		}
		if( empty($this -> accessToken) ){
			$this -> accessToken = $this -> getAccessToken();
		}
	}
	
	/**
	 * 获取页面二维码
	 * @param	$page	小程序页面路径
	 */
//	public function getPageQrcode( $page = null ){
//		if( is_null($code) ) return FALSE;
//		$url = $this -> url['getPageQrcode'];
//		$url = str_replace('{ACCESS_TOKEN}', $this -> accessToken, $url);
//		$param = [
//			'scene'=>'test',
//			'page'=>$page
//		];
//		$re = requestPost($url, json_encode($param));
//		$re = json_decode($re, TRUE);
//		if( $re['errcode'] != 0 ){
//			$outArr['code'] = '50003';
//			$outArr['info'] = $re;
//			returnResults($outArr);
//		}
//		return $re;
//		return $outArr;
//	}
	
	/**
	 * 获取用户openid
	 * @param	$code	小程序获取到的用户code
	 */
	public function getUserOpenId( $code = null ){
		if( is_null($code) ) return FALSE;
		$url = $this -> url['getOpenId'];
		$url = str_replace('{APPID}', $this -> appId, $url);
		$url = str_replace('{SECRET}', $this -> secret, $url);
		$url = str_replace('{JSCODE}', $code, $url);
		$re = requestGet($url);
		$re = json_decode($re, TRUE);
		if( !isset($re['openid']) || empty($re['openid']) ){
			$outArr['code'] = '50003';
			$outArr['info'] = $re;
		}else{
			$outArr['code'] = '1';
			$outArr['info'] = $re;
		}
		return $outArr;
	}
	
	/**
	 * 获取用户手机号
	 */
	public function getUserPhone( $code = null ){
		if( is_null($code) ) return FALSE;
		$url = $this -> url['getUserPhone'];
		$url = str_replace('{ACCESS_TOKEN}', $this -> accessToken, $url);
		$param = [
			'code'=>$code
		];
		$re = requestPost($url, json_encode($param));
		$re = json_decode($re, TRUE);
		if( $re['errcode'] != 0 ){
			$outArr['code'] = '50003';
			$outArr['info'] = $re;
			returnResults($outArr);
		}
		return $re;
	}
	
	/**
	 * 获取accessToken
	 */
	public function getAccessToken(){
		$accessToken = controller("service/Cache") -> getCache("wechatMinAccessToken");
		$accessToken = '';
		if( !$accessToken || empty($accessToken) ){
			$url = $this -> url['getAccessToken'];
			$url = str_replace('{APPID}', $this -> appId, $url);
			$url = str_replace('{APPSECRET}', $this -> secret, $url);
			$re = requestGet($url);
			$re = json_decode($re, TRUE);
			if( !$re['access_token'] || empty($re['access_token']) ){
				return FALSE;
			}
			controller("service/Cache") -> setCache("wechatMinAccessToken", "", $re['access_token'], 7000);
			$accessToken = $re['access_token'];
		}
		return $accessToken;
	}
}
